package com.bw.controller;

import com.alibaba.fastjson.JSONObject;
import com.bw.util.UploadUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/file/*")
public class FileController extends BaseController{
    //1.图片上传
    protected void upload(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 服务器地址
        String s = UploadUtil.uploadImage(req, resp);
        resp.getWriter().println(JSONObject.toJSONString(s));
    }
    //2.图片回显
    protected void showImg(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //图片路径 接收服务器路径 不接受json了
        String imgUrl = req.getParameter("imgUrl");
        //调用工具类 传参1.图片服务器路径 2. resp转换后的二进制数据传给src
        UploadUtil.showImg(imgUrl,resp);
    }
}
